package org.destiny.swing.mapping.base;

import java.awt.FlowLayout;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JPanel;

import org.destiny.swing.annotation.ArrayElement;
import org.destiny.swing.annotation.IArrayProducer;

public class CheckBoxGroupMapper<AT> extends AbstractFormItemMapper<AT[]>{

	JPanel checkBoxPane;
	public CheckBoxGroupMapper(Field field) {
		super(field);
		checkBoxPane = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));
		JCheckBox checkBox;
		// getArray(Field field, Object object)
		try {
			Method getArrayMethod = IArrayProducer.class.getMethod("getArray", Field.class, Object.class);
			Object[] ary = (Object[]) getArrayMethod.invoke(formItem.optionsProducer().newInstance(), field, null);
			for (Object element : ary) {
				ArrayElement aryEle = (ArrayElement) element;
				checkBox = new JCheckBox(aryEle.name);
				checkBoxPane.add(checkBox);
			}
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		}
	}

	@Override
	public JComponent getInputComponent() {
		return checkBoxPane;
	}

	@Override
	public AT[] getFieldValue() {
		return null;
	}

	@Override
	public void setFieldValue(AT[] fieldValue) {
		
	}

}
